package Tree;

import com.sun.org.apache.xpath.internal.objects.XString;

public class TreeNode {
    private int id;
    private String name;
    private TreeNode left;
    private TreeNode right;

    public TreeNode( int id, String name){
        this.id = id;
        this.name = name;
    }

    public int getId () {
        return id;
    }

    public void setId ( int id){
        this.id = id;
    }

    public String getName () {
        return name;
    }

    public void setName (String name){
        this.name = name;
    }

    public TreeNode getLeft () {
        return left;
    }

    public void setLeft (TreeNode left){
        this.left = left;
    }

    public TreeNode getRight () {
        return right;
    }

    public void setRight (TreeNode right){
        this.right = right;
    }



    /**
     * 前序遍历
     */
    public void frontErgodic(){
        System.out.println(this.getId());
        if(this.getLeft()!=null){
            this.getLeft().frontErgodic();
        }
        if(this.getRight()!=null){
            this.getRight().frontErgodic();
        }

    }

    /**
     * 中序遍历
     */
    public void middleErgodic(){
        if(this.getLeft()!=null){
            this.getLeft().middleErgodic();
        }
        System.out.println(this.getId());
        if(this.getRight()!=null){
            this.getRight().middleErgodic();
        }
    }

    /**
     * 后序遍历
     */
    public void behindErgodic(){
        if(this.getLeft()!=null){
            this.getLeft().behindErgodic();
        }

        if(this.getRight()!=null){
            this.getRight().behindErgodic();
        }
        System.out.println(this.getId());
    }

    /**
     * 根据id进行查询-前序遍历
     * @param id
     * @return
     */
    public TreeNode preSearch(int id){
        if(this.getId()==id){
            return this;
        }
        TreeNode node=null;
        if(this.getLeft()!=null){
            node = this.getLeft().preSearch(id);
        }
        if(node!=null){
            return  node;
        }
        if(this.getRight()!=null){
            node = this.getRight().preSearch(id);
        }
        return node;
    }

    /**
     * 根据id进行查询-中序遍历
     * @param id
     * @return
     */
    public TreeNode middleSearch(int id){

        TreeNode node=null;
        if(this.getLeft()!=null){
            node = this.getLeft().middleSearch(id);
        }
        if(node!=null){
            return  node;
        }
        if(this.getId()==id){
            return this;
        }
        if(this.getRight()!=null){
            node = this.getRight().middleSearch(id);
        }
        return node;
    }

    /**
     * 根据id进行查询-后序遍历
     * @param id
     * @return
     */
    public TreeNode behindSearch(int id){

        TreeNode node=null;
        if(this.getLeft()!=null){
            node = this.getLeft().middleSearch(id);
        }
        if(node!=null){
            return  node;
        }

        if(this.getRight()!=null){
            node = this.getRight().middleSearch(id);
        }
        if(this.getId()==id){
            return this;
        }

        return node;
    }

    /**
     * 添加新的节点
     * @param treeNode
     */
    public void addNode(TreeNode treeNode){
        boolean flag = false;
        if(this.getLeft() != null && this.getRight() != null){
            flag = true;
        }else if(this.getLeft() == null){
            this.setLeft(treeNode);
        }else if(this.getRight() == null){
            this.setRight(treeNode);
        }

        if (flag){
            if (this.getLeft().getId() <= this.getRight().getId()){
                this.getLeft().addNode(treeNode);
            }else {
                this.getRight().addNode(treeNode);
            }
        }

    }

    /**
     * 根据id进行删除某个节点,
     * 只会删除单个叶子节点，如果删除的节点不是叶子节点，则将该节点下的子节点合并到主树中
     * @param id
     */
    public TreeNode delNode(int id){
        TreeNode treeNode = null;
        boolean flag = false;

        if(this.getId() == id){
            if(this.getLeft() != null && this.getRight() != null){
                flag = true;
            }else if(this.getLeft() != null){
                treeNode = this.getLeft();
            }else if(this.getRight() != null){
                treeNode = this.getRight();
            }

            if (flag){
                if ( this.getLeft().getId() >= this.getRight().getId() ){
                    treeNode = this.getRight();
                    treeNode.addNode( this.getLeft() );
                }else {
                    treeNode = this.getLeft();
                    treeNode.addNode( this.getRight() );
                }
            }

            return  treeNode;
        }else {
            if (this.getLeft() != null){
                this.setLeft( this.getLeft().delNode(id) );
            }

            if (this.getRight() != null){
                this.setRight( this.getRight().delNode(id) );
            }

            return  this;
        }

    }

    /**
     * 根据id删除节点，
     * 删除单个叶子节点，如果是非叶子节点，则该节点下的子节点也全部删除
     * @param id
     */
    public void delNodeALL(int id){
        if( this.getLeft() != null){
            if (this.getLeft().getId() == id){
                this.setLeft(null);
                return;
            }else {
                this.getLeft().delNodeALL(id);
            }
        }

        if(this.getRight() != null){
            if (this.getRight().getId() == id){
                this.setRight(null);
                return;
            }else {
                this.getRight().delNodeALL(id);
            }
        }
    }

}

